<?php

/*
 * parseHtml.php
 * Author: Carlos Costa Jordao
 * Email: carlosjordao@yahoo.com
 *
 * My notation of variables:
 * i_ = integer, ex: i_count
 * a_ = array, a_html
 * b_ = boolean,
 * s_ = string
 *
 * What it does:
 * - parses a html string and get the tags
 * - exceptions: html tags like <br> <hr> </a>, etc
 * - At the end, the array will look like this:
 * ["IMG"][0]["SRC"] = "xxx"
 * ["IMG"][1]["SRC"] = "xxx"
 * ["IMG"][1]["ALT"] = "xxx"
 * ["A"][0]["HREF"] = "xxx"
 *
 */

function parseHtml($s_str, $tag=NULL, $item_number=NULL, $list_tag=0, $strUpper=0) {
    $i_indicatorL = 0;
    $i_indicatorR = 0;
    $s_tagOption = "";
    $i_arrayCounter = 0;
    $a_html = array();
// Search for a tag in string
    while (is_int(($i_indicatorL = strpos($s_str, "<", $i_indicatorR)))) {
// Get everything into tag...
        $i_indicatorL++;
        $i_indicatorR = strpos($s_str, ">", $i_indicatorL);
        $s_temp = substr($s_str, $i_indicatorL, ($i_indicatorR - $i_indicatorL));
        $a_tag = explode(' ', $s_temp);
        
// Here we get the tag's name
        list(, $s_tagName,, ) = each($a_tag);
        if ($strUpper)
            $s_tagName = strtoupper($s_tagName);
// Well, I am not interesting in <br>, </font> or anything else like that...
// So, this is false for tags without options.
        $b_boolOptions = is_array(($s_tagOption = each($a_tag))) && $s_tagOption[1];
        if ($b_boolOptions) {
// Without this, we will mess up the array
            $i_arrayCounter = 0;
            if (isset($a_html[$s_tagName]))
                $i_arrayCounter = (int) count($a_html[$s_tagName]);
// get the tag options, like src="htt://". Here, s_tagTokOption is 'src' and s_tagTokValue is '"http://"'

            do {
                $s_tagTokOption = $strUpper ? strtoupper(strtok($s_tagOption[1], "=")) : strtok($s_tagOption[1], "=");
                $s_tagTokValue = trim(strtok("="));
                if ($tag) {
                    if ($a_tag[0] == $tag)
                        $a_html[$s_tagName][$i_arrayCounter][$s_tagTokOption] = $s_tagTokValue;
                }
                else {
                    $a_html[$s_tagName][$i_arrayCounter][$s_tagTokOption] = $s_tagTokValue;
                }
                $b_boolOptions = is_array(($s_tagOption = each($a_tag))) && $s_tagOption[1];
            } while ($b_boolOptions);
        }
    }
    $item_count = 1;
    $return = array();
    if ($item_number) {
        for ($i = 0; $i < $item_number; $i++) {
            if (!$list_tag) {
                if ($i == $item_number - 1) {
                    if (isset($a_html[$tag][$i]) && $a_html[$tag][$i])
                        $return[] = $a_html[$tag][$i];
                }
            }else {
                if (isset($a_html[$tag][$i]) && $a_html[$tag][$i])
                    $return[] = $a_html[$tag][$i];
            }
        }
    }else {
        $return = $a_html;
    }
    return $return;
}

$html = '
    <img src="images/Clair/section/no1/issue_tn_001.jpg" border="0" A="1" />adsfasdfsdaf<img src="images/Clair/section/no1/cover0001.jpg" border="0" />dsafsdf
    <img src="images/Clair/section/no1/cover_hilight_0001.png" border="0" />
    <div a="123">adsf</div>
    <div>asdfadsdddd</div>
    <div>34567857697</div>';
var_dump(parseHtml($html, "img", 2,1));



function strip_only($str, $tags, $stripContent = false) {
    $content = '';
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) {
        if ($stripContent)
             $content = '(.+</'.$tag.'[^>]*>|)';
         $str = preg_replace('#</?'.$tag.'[^>]*>'.$content.'#is', '', $str);
    }
    return $str;
}

$str = '<font color="red"><b>red</b></font> text <br /><img alt="asdf" src="" />';
$tags = 'img';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
echo $a;
echo $b;
?>
